﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using HomeBuyerTools.FinanceProperties;

namespace HomeBuyerTools.Views.CompareLoansByTerm
{
    public partial class Result : PhoneApplicationPage
    {
        public Result()
        {
            InitializeComponent();
        }
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string pb = "";
            string ir = "";
            NavigationContext.QueryString.TryGetValue("pb", out pb);
            NavigationContext.QueryString.TryGetValue("ir", out ir);
            double princbalance = Convert.ToDouble(pb);
            double interestrate = Convert.ToDouble(ir);

            List<CompareLoanByTerm> objTermList = new List<CompareLoanByTerm>();

            int StartYear = 10;
            List<int> yearsDuration = new List<int>();
            for (int i = 1; i <= 5; i++)
            {
                yearsDuration.Add(StartYear);
                StartYear = StartYear + 5;
                //startInterestRate = startInterestRate + interestRatio;
            }

           
            foreach (int yrs in yearsDuration)
            {
                CompareLoanByTerm objTerm = new CompareLoanByTerm();
                double totalinterest = 0.0;
                double MonthlyPayment = 0.0;
                double Interest = Math.Round(princbalance * (interestrate / 100 / 12), 2);
                double payment = Math.Round((princbalance * (interestrate / 100 / 12)) / (1 - (Math.Pow(1 / (1 + (interestrate / 100 / 12)), yrs*12))), 2);
                double principle = Math.Round(payment - Interest, 2);
                double balance = Math.Round(princbalance - principle, 2);
                MonthlyPayment = payment;

                totalinterest = Interest;
                for (int i = 1; i <= yrs*12; i++)
                {
                    Interest = Math.Round(balance * (interestrate / 100 / 12), 2);
                    payment = Math.Round((balance * (interestrate / 100 / 12)) / (1 - (Math.Pow(1 / (1 + (interestrate / 100 / 12)), (yrs*12 - i)))), 2);
                    principle = Math.Round(payment - Interest, 2);
                    balance = Math.Round(balance - principle, 2);
                    totalinterest = totalinterest + Interest;
                }



                //objTerm.InterestRate = indRate.ToString();
                objTerm.TermOfMortgage = yrs.ToString();
                objTerm.MonthlyPayments = MonthlyPayment.ToString();
                objTerm.TotalInterest = totalinterest.ToString();
                objTerm.TotalPrinciple = princbalance.ToString();
                objTerm.TotalPayments = (princbalance + totalinterest).ToString();

                objTermList.Add(objTerm);
            }
            lbxComLoanByTerm.ItemsSource = objTermList;
        
           

        }
    }
}